package edu.ou.herbarium.threads2;


import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;

import javax.imageio.ImageIO;

import edu.ou.herbarium.image.ImageUtils;
import edu.ou.herbarium.sharedata.ErrorData;
import edu.ou.herbarium.sharedata.ShareData2;
import edu.ou.herbarium.sharedata.ShareData6;
import edu.ou.herbarium.sharedata.ShareData7;
import edu.ou.herbarium.sharedata.ShareData9;
import edu.ou.herbarium.sharedata.TransferData6;
import edu.ou.herbarium.sharedata.TransferData7;
import edu.ou.herbarium.sharedata.TransferData9;
import edu.ou.herbarium.wsclient.DataUtilsDelegate;
import edu.ou.herbarium.wsclient.DataUtilsService;
import edu.ou.herbarium.wsclient.Specimen;

public class FileThread extends Thread{
	ErrorData de;
	ShareData6 s6;
	ShareData7 s7;
	ShareData9 s9;
	
	public FileThread(ErrorData de, ShareData6 s6,ShareData7 s7, ShareData9 s9){
		this.de = de;
		this.s6 = s6;
		this.s7 = s7;
		this.s9 = s9;
	}
	
	public long copyFile2(String srcFileName, String newFileName) {  
		File srcFile = new File(srcFileName);
        long copySizes = 0;  
        if (!srcFile.exists()) {  
            copySizes = -1;  
        } else if (newFileName == null) {  
            copySizes = -1;  
        } else {  
            try {  
                FileChannel fcin = new FileInputStream(srcFile).getChannel();  
                FileChannel fcout = new FileOutputStream(new File(newFileName)).getChannel();  
                long size = fcin.size();  
                fcin.transferTo(0, fcin.size(), fcout);  
                fcin.close();  
                fcout.close();  
                copySizes = size;  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        return copySizes;  
    }  
	
	public void run(){
		ImageUtils util = new ImageUtils();
		try{
			while(true){
				TransferData6 d6 = s6.getShareData();
				String originalFilePath = d6.getOriginalFileName();
	//			String scaledFilePath = d6.getThumbnailFileName();
				String scaledFilePath = originalFilePath;
				
	//			copyFile2(originalFilePath+".jpg",scaledFilePath+"_1.jpg");test
				util.rotate(originalFilePath+"full.jpg");
				
				File file = new File(originalFilePath+"full.jpg");
				BufferedImage hugeImage = ImageIO.read(file);
				int red = hugeImage.getRGB(506, 5389);
				Color redcolor = new Color(red, true);
				int r = redcolor.getRed();
				int g = redcolor.getGreen();
				int b = redcolor.getBlue();
				double er = errorRate(r,g,b,255,0,0);
				if(er>0.01) de.setShareData("ERROR_COLOR_0:: Red color error with a rate "+er);
				
				int green = hugeImage.getRGB(460, 5389);
				Color greencolor = new Color(green, true);
				 r = greencolor.getRed();
				 g = greencolor.getGreen();
				 b = greencolor.getBlue();
				double eg = errorRate(r,g,b,0,255,0);
				if(eg>0.01) de.setShareData("ERROR_COLOR_1:: Green color error with a rate "+eg);
				
				int blue = hugeImage.getRGB(425, 5389);
				Color bluecolor = new Color(blue, true);
				 r = bluecolor.getRed();
				 g = bluecolor.getGreen();
				 b = bluecolor.getBlue();
				double eb = errorRate(r,g,b,0,0,255);
				if(eb>0.01) de.setShareData("ERROR_COLOR_2:: Blue color error with a rate "+eb);
				
				System.out.println(er+" "+eg+" "+eb);
				util.scale(originalFilePath+"full.jpg", scaledFilePath+"full_2.jpg", 2, false);
				util.scale(originalFilePath+"full_2.jpg", scaledFilePath+"full_4.jpg", 2, false);
				util.scale(originalFilePath+"full_4.jpg", scaledFilePath+"full_8.jpg", 2, false);
				
				
				//feedback to client
				d6.getS2().setShareData(originalFilePath+"full.jpg");
				
				byte[] bytes = getbytes(new File(originalFilePath+"full.jpg"));
				byte[] bytes2 = getbytes(new File(originalFilePath+"full_2.jpg"));
				byte[] bytes3 = getbytes(new File(originalFilePath+"full_4.jpg"));
				byte[] bytes4 = getbytes(new File(originalFilePath+"full_8.jpg"));
			
			
				TransferData7 d7 = new TransferData7(d6.getSpec(),bytes,bytes2,bytes3,bytes4,originalFilePath);
				TransferData9 d9 = new TransferData9(d6.getSpec(),originalFilePath,originalFilePath,"checksum");
				s7.setShareData(d7);
				s9.setShareData(d9);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public double errorRate(int r,int g,int b,int sr,int sg,int sb){
		double res = 0.0;
		double real = (Math.abs(r-sr)+Math.abs(g-sg)+Math.abs(b-sb));
		double standard = (255+255+255);
		res = real/standard;
		return res;
	}
	public int abv(int i){
		if(i>=0) return i;
		else return -i;
	}
	
	public byte[] getbytes(File file){
		byte[] bytes = null;
		FileInputStream fin = null;
		FileChannel ch = null;
		try {
			fin = new FileInputStream(file);
			ch = fin.getChannel();
			int size = (int) ch.size();
			MappedByteBuffer buf = ch.map(MapMode.READ_ONLY, 0, size);
			bytes = new byte[size];
			buf.get(bytes);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bytes;
	}
}
